﻿using UnityEngine;
using System.Collections.Generic;

public class GameCtr : MonoBehaviour {
	public List<CardInfo> selfLineup;
	public List<CardInfo> rivalLineup;

	// Use this for initialization
	void Start () {
		//读取双方阵容
		List<CardInfo> selflineup = getSelfLineup ();
		for (int i=0; i<selflineup.Count; i++) {
			MilitaryUnit unit = selflineup[i].Instantiate();
			Vector3 startPosition = startPos(i, true);
			unit.move(startPosition, startPosition + new Vector3(8, 0, 0));
		}

		List<CardInfo> rivallineup = getRivalLineup ();
		for (int i=0; i<rivallineup.Count; i++) {
			MilitaryUnit unit = rivallineup[i].Instantiate();
			Vector3 startPosition = startPos(i, false);
			unit.move(startPosition, startPosition + new Vector3(-8, 0, 0));
		}
	}
	
	// Update is called once per frame
	void Update () {
	
	}

	private List<CardInfo> getRivalLineup()
	{
		if (rivalLineup == null) {
			return new List<CardInfo>();	
		}

		return rivalLineup;
	}

	private List<CardInfo> getSelfLineup()
	{
		if (selfLineup == null) {
			return new List<CardInfo>();
		}

		return selfLineup;
	}

	private Vector3 startPos(int index, bool isSelf)
	{
		Vector3 pos;

		if (isSelf) {
			Vector3 basePos = new Vector3(-15f, 0f, 2f);
			Vector2 coordinate = index2Coordinate(index);
			pos = basePos + new Vector3(-3.5f * coordinate.y, 0, -2.5f *coordinate.x);
		}
		else {
			Vector3 basePos = new Vector3(15f, 0f, -2f);
			Vector2 coordinate = index2Coordinate(index);
			pos = basePos + new Vector3(3.5f * coordinate.y, 0, 2.5f *coordinate.x);
		}

		return pos;
	}

	private Vector2 index2Coordinate(int index)
	{
		Vector2 coordinate;

		coordinate.x = index % 3;
		coordinate.y = index/3;

		return coordinate;
	}
}
